package com.irdstudio.efp.riskm.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.FileOperateUtil;
import com.irdstudio.efp.riskm.service.dao.RscWarnAccLoanDao;
import com.irdstudio.efp.riskm.service.domain.RscWarnAccLoan;
import com.irdstudio.efp.riskm.service.facade.RscWarnAccLoanService;
import com.irdstudio.efp.riskm.service.vo.RscWarnAccLoanVO;
/**
 * <p>ServiceImpl: 业务数据信息表				<p>
 * @author chenqm1
 * @date 2018-05-30
 */
@Service("rscWarnAccLoanService")
public class RscWarnAccLoanServiceImpl implements RscWarnAccLoanService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(RscWarnAccLoanServiceImpl.class);

	@Autowired
	private RscWarnAccLoanDao rscWarnAccLoanDao;
	
	/**
	 * 新增操作
	 */
	public int insertRscWarnAccLoan(RscWarnAccLoanVO inRscWarnAccLoanVo) {
		logger.debug("当前新增数据为:"+ inRscWarnAccLoanVo.toString());
		int num = 0;
		try {
			RscWarnAccLoan rscWarnAccLoan = new RscWarnAccLoan();
			beanCopy(inRscWarnAccLoanVo, rscWarnAccLoan);
			num = rscWarnAccLoanDao.insertRscWarnAccLoan(rscWarnAccLoan);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(RscWarnAccLoanVO inRscWarnAccLoanVo) {
		logger.debug("当前删除数据条件为:"+ inRscWarnAccLoanVo);
		int num = 0;
		try {
			RscWarnAccLoan rscWarnAccLoan = new RscWarnAccLoan();
			beanCopy(inRscWarnAccLoanVo, rscWarnAccLoan);
			num = rscWarnAccLoanDao.deleteByPk(rscWarnAccLoan);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnAccLoanVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(RscWarnAccLoanVO inRscWarnAccLoanVo) {
		logger.debug("当前修改数据为:"+ inRscWarnAccLoanVo.toString());
		int num = 0;
		try {
			RscWarnAccLoan rscWarnAccLoan = new RscWarnAccLoan();
			beanCopy(inRscWarnAccLoanVo, rscWarnAccLoan);
			num = rscWarnAccLoanDao.updateByPk(rscWarnAccLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnAccLoanVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public RscWarnAccLoanVO queryByPk(RscWarnAccLoanVO inRscWarnAccLoanVo) {
		
		logger.debug("当前查询参数信息为:"+ inRscWarnAccLoanVo);
		try {
			RscWarnAccLoan queryRscWarnAccLoan = new RscWarnAccLoan();
			beanCopy(inRscWarnAccLoanVo, queryRscWarnAccLoan);
			RscWarnAccLoan queryRslRscWarnAccLoan = rscWarnAccLoanDao.queryByPk(queryRscWarnAccLoan);
			if (Objects.nonNull(queryRslRscWarnAccLoan)) {
				RscWarnAccLoanVO outRscWarnAccLoanVo = beanCopy(queryRslRscWarnAccLoan, new RscWarnAccLoanVO());
				logger.debug("当前查询结果为:"+ outRscWarnAccLoanVo.toString());
				return outRscWarnAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public void fileDataInsertTable(File file, RscWarnAccLoanVO clazz, String operation) {
		try {
			List<RscWarnAccLoanVO> readTxt = FileOperateUtil.readTxt(file,clazz.getClass(),operation);
			if(readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = 3;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
					List<RscWarnAccLoanVO> subList = new ArrayList<>();
					if(end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					}else {
						subList = readTxt.subList(start, end);
					}
					for (RscWarnAccLoanVO vo : subList) {
						RscWarnAccLoan rscWarnAccloan = new RscWarnAccLoan();
						beanCopy(vo, rscWarnAccloan);
						int num = rscWarnAccLoanDao.insertRscWarnAccLoan(rscWarnAccloan);
						insertNum = insertNum + num;
					}
				}
				logger.info("文件中数据成功插入数量为:" + insertNum);
			}
		} catch (Exception e) {
			logger.error("文件数据插入表过程出错", e);
		}
	}
	/**
	 * 客户是否存在行内逾期记录
	 * @param rscWarnAccLoanVo
	 * @return 存在：true，不存在：false
	 */
	@Override
	public boolean isExistOverdue(RscWarnAccLoanVO rscWarnAccLoanVo) {
		boolean result = true;
		logger.info("判断客户是否存在行内逾期记录传入参数："+rscWarnAccLoanVo.toString());
		try {
			
		}catch(Exception e) {
			result = false;
			logger.error("判断客户是否存在逾期记录异常",e);
		}
		return result;
	}
	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RscWarnAccLoanVO> queryAllOwner(RscWarnAccLoanVO rscWarnAccLoanVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnAccLoanVO> list = null;
		try {
			List<RscWarnAccLoan> rscWarnAccLoans = rscWarnAccLoanDao.queryAllOwnerByPage(rscWarnAccLoanVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnAccLoans.size());
			pageSet(rscWarnAccLoans, rscWarnAccLoanVo);
			list = (List<RscWarnAccLoanVO>) beansCopy(rscWarnAccLoans, RscWarnAccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public RscWarnAccLoanVO queryBybillNo(RscWarnAccLoanVO rscWarnAcc) {
		
		logger.debug("当前查询参数信息为:"+ rscWarnAcc);
		try {
			RscWarnAccLoan queryRscWarnAccLoan = new RscWarnAccLoan();
			beanCopy(rscWarnAcc, queryRscWarnAccLoan);
			RscWarnAccLoan queryRslRscWarnAccLoan = rscWarnAccLoanDao.queryBybillNo(queryRscWarnAccLoan);
			if (Objects.nonNull(queryRslRscWarnAccLoan)) {
				RscWarnAccLoanVO outRscWarnAccLoanVo = beanCopy(queryRslRscWarnAccLoan, new RscWarnAccLoanVO());
				logger.debug("当前查询结果为:"+ outRscWarnAccLoanVo.toString());
				return outRscWarnAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
}
